home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / enriched.el.z / enriched.el
Encoding:
Text File  |  1998-05-21  |  17.8 KB  |  531 lines

  1. ;;; enriched.el --- read and save files in text/enriched format
  2.  
  3. ;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; XEmacs version: Mike Sperber <sperber@informatik.uni-tuebingen.de>
  6. ;; Original author: Boris Goldowsky <boris@gnu.ai.mit.edu>
  7. ;; Keywords: wp, faces
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 20.2.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This file implements reading, editing, and saving files with
  31. ;; text-properties such as faces, levels of indentation, and true line
  32. ;; breaks distinguished from newlines just used to fit text into the window.
  33.  
  34. ;; The file format used is the MIME text/enriched format, which is a
  35. ;; standard format defined in internet RFC 1563.  All standard annotations
  36. ;; are supported except for <smaller> and <bigger>, which are currently not
  37. ;; possible to display.
  38.  
  39. ;; A separate file, enriched.doc, contains further documentation and other
  40. ;; important information about this code.  It also serves as an example
  41. ;; file in text/enriched format.  It should be in the etc directory of your
  42. ;; emacs distribution.
  43.  
  44. ;;; TODO for the XEmacs port:
  45. ;;
  46. ;; Currently XEmacs does not support default-text-properties.  The
  47. ;; original enriched.el uses this to set the left-margin,
  48. ;; right-margin, and justification properties to 'front-sticky.
  49. ;; If you know the Right Way to fix this, contact
  50. ;; Mike Sperber <sperber@informatik.uni-tuebingen.de>.
  51.  
  52. ;;; Code:
  53.  
  54. (provide 'enriched)
  55.  
  56. ;;;
  57. ;;; Variables controlling the display
  58. ;;;
  59.  
  60. (defgroup enriched nil
  61.   "Read and save files in text/enriched format"
  62.   :group 'wp)
  63.  
  64. (defcustom enriched-verbose t
  65.   "*If non-nil, give status messages when reading and writing files."
  66.   :type 'boolean
  67.   :group 'enriched)
  68.  
  69. ;;;
  70. ;;; Set up faces & display table
  71. ;;;
  72.  
  73. ;; Emacs doesn't have a "fixed" face by default, since all faces currently
  74. ;; have to be fixed-width.  So we just pick one that looks different from the
  75. ;; default.
  76. (defface fixed
  77.   '((t (:bold t)))
  78.   "Face used for text that must be shown in fixed width.
  79. Currently, emacs can only display fixed-width fonts, but this may change.
  80. This face is used for text specifically marked as fixed-width, for example
  81. in text/enriched files."
  82.   :group 'enriched)
  83.  
  84. (defface excerpt
  85.   '((t (:italic t)))
  86.   "Face used for text that is an excerpt from another document.
  87. This is used in enriched-mode for text explicitly marked as an excerpt."
  88.   :group 'enriched)
  89.  
  90. (defconst enriched-display-table
  91.   ;; XEmacs change
  92.   ;; (or (copy-sequence standard-display-table)
  93.   ;;     (make-display-table)))
  94.   (make-display-table))
  95. (aset enriched-display-table ?\f (make-vector (1- (frame-width)) ?-))
  96.  
  97. (defconst enriched-par-props '(left-margin right-margin justification)
  98.   "Text-properties that usually apply to whole paragraphs.
  99. These are set front-sticky everywhere except at hard newlines.")
  100.  
  101. ;;;
  102. ;;; Variables controlling the file format
  103. ;;;   (bidirectional)
  104.  
  105. (defconst enriched-initial-annotation
  106.   (lambda ()
  107.     (format "Content-Type: text/enriched\nText-Width: %d\n\n"
  108.         fill-column))
  109.   "What to insert at the start of a text/enriched file.
  110. If this is a string, it is inserted.  If it is a list, it should be a lambda
  111. expression, which is evaluated to get the string to insert.")
  112.  
  113. (defconst enriched-annotation-format "<%s%s>"
  114.   "General format of enriched-text annotations.")
  115.  
  116. (defconst enriched-annotation-regexp "<\\(/\\)?\\([-A-za-z0-9]+\\)>"
  117.   "Regular expression matching enriched-text annotations.")
  118.  
  119. (defconst enriched-translations
  120.   '((face          (bold-italic "bold" "italic")
  121.            (bold        "bold")
  122.            (italic      "italic")
  123.            (underline   "underline")
  124.            (fixed       "fixed")
  125.            (excerpt     "excerpt")
  126.            (default     )
  127.            (nil         enriched-encode-other-face))
  128.     (left-margin   (4           "indent"))
  129.     (right-margin  (4           "indentright"))
  130.     (justification (none        "nofill")
  131.            (right       "flushright")
  132.            (left        "flushleft")
  133.            (full        "flushboth")
  134.            (center      "center"))
  135.     (PARAMETER     (t           "param")) ; Argument of preceding annotation
  136.     ;; The following are not part of the standard:
  137.     (FUNCTION      (enriched-decode-foreground "x-color")
  138.            (enriched-decode-background "x-bg-color")
  139.            ;; XEmacs addition
  140.            (facemenu-make-larger "bigger")
  141.            (facemenu-make-smaller "smaller"))
  142.     (read-only     (t           "x-read-only"))
  143.     (unknown       (nil         format-annotate-value))
  144. ;   (font-size     (2           "bigger")       ; unimplemented
  145. ;                  (-2          "smaller"))
  146. )
  147.   "List of definitions of text/enriched annotations.
  148. See `format-annotate-region' and `format-deannotate-region' for the definition
  149. of this structure.")
  150.  
  151. (defconst enriched-ignore
  152.   '(front-sticky rear-nonsticky hard)
  153.   "Properties that are OK to ignore when saving text/enriched files.
  154. Any property that is neither on this list nor dealt with by
  155. `enriched-translations' will generate a warning.")
  156.  
  157. ;;; Internal variables
  158.  
  159. (defvar enriched-mode nil
  160.   "True if Enriched mode is in use.")
  161. (make-variable-buffer-local 'enriched-mode)
  162.  
  163. (if (not (assq 'enriched-mode minor-mode-alist))
  164.     (setq minor-mode-alist
  165.       (cons '(enriched-mode " Enriched")
  166.         minor-mode-alist)))
  167.  
  168. (defcustom enriched-mode-hook nil
  169.   "Functions to run when entering Enriched mode.
  170. If you set variables in this hook, you should arrange for them to be restored
  171. to their old values if you leave Enriched mode.  One way to do this is to add
  172. them and their old values to `enriched-old-bindings'."
  173.   :type 'hook
  174.   :group 'enriched)
  175.  
  176. (defvar enriched-old-bindings nil
  177.   "Store old variable values that we change when entering mode.
  178. The value is a list of \(VAR VALUE VAR VALUE...).")
  179. (make-variable-buffer-local 'enriched-old-bindings)
  180.  
  181. ;;;
  182. ;;; Define the mode
  183. ;;;
  184.  
  185. ;;;###autoload
  186. (defun enriched-mode (&optional arg)
  187.   "Minor mode for editing text/enriched files.
  188. These are files with embedded formatting information in the MIME standard
  189. text/enriched format.
  190. Turning the mode on runs `enriched-mode-hook'.
  191.  
  192. More information about Enriched mode is available in the file 
  193. etc/enriched.doc  in the Emacs distribution directory.
  194.  
  195. Commands:
  196.  
  197. \\<enriched-mode-map>\\{enriched-mode-map}"
  198.   (interactive "P")
  199.   (let ((mod (buffer-modified-p)))
  200.     (cond ((or (<= (prefix-numeric-value arg) 0)
  201.            (and enriched-mode (null arg)))
  202.        ;; Turn mode off
  203.        (setq enriched-mode nil)
  204.        (setq buffer-file-format (delq 'text/enriched buffer-file-format))
  205.        ;; restore old variable values
  206.        (while enriched-old-bindings
  207.          (funcall 'set (car enriched-old-bindings)
  208.               (car (cdr enriched-old-bindings)))
  209.          (setq enriched-old-bindings (cdr (cdr enriched-old-bindings)))))
  210.  
  211.       (enriched-mode nil)        ; Mode already on; do nothing.
  212.  
  213.       (t (setq enriched-mode t)    ; Turn mode on
  214.          (add-to-list 'buffer-file-format 'text/enriched)
  215.          ;; Save old variable values before we change them.
  216.          ;; These will be restored if we exit Enriched mode.
  217.          (setq enriched-old-bindings
  218.            ;; XEmacs change
  219.            (list ; 'buffer-display-table buffer-display-table
  220.              'indent-line-function indent-line-function
  221.              'default-text-properties default-text-properties))
  222.          (make-local-variable 'indent-line-function)
  223.          (make-local-variable 'default-text-properties)
  224.          (setq indent-line-function 'indent-to-left-margin
  225.             ;; XEmacs change
  226.             ;; buffer-display-table  enriched-display-table
  227.            )
  228.          (use-hard-newlines 1 nil)
  229.          (let ((sticky (plist-get default-text-properties 'front-sticky))
  230.            (p enriched-par-props))
  231.            (while p
  232.          (add-to-list 'sticky (car p))
  233.          (setq p (cdr p)))
  234.            (if sticky
  235.            (setq default-text-properties
  236.              (plist-put default-text-properties
  237.                     'front-sticky sticky))))
  238.          (run-hooks 'enriched-mode-hook)))
  239.     (set-buffer-modified-p mod)
  240.     ;; XEmacs change
  241.     (redraw-modeline)))
  242.  
  243. ;;;
  244. ;;; Keybindings
  245. ;;;
  246.  
  247. (defvar enriched-mode-map nil
  248.   "Keymap for Enriched mode.")
  249.  
  250. (if (null enriched-mode-map)
  251.     (fset 'enriched-mode-map (setq enriched-mode-map (make-sparse-keymap))))
  252.  
  253. (if (not (assq 'enriched-mode minor-mode-map-alist))
  254.     (setq minor-mode-map-alist
  255.       (cons (cons 'enriched-mode enriched-mode-map)
  256.         minor-mode-map-alist)))
  257.  
  258. (define-key enriched-mode-map "\C-a" 'beginning-of-line-text)
  259. (define-key enriched-mode-map "\C-m" 'reindent-then-newline-and-indent)
  260. (define-key enriched-mode-map "\C-j" 'reindent-then-newline-and-indent)
  261. (define-key enriched-mode-map "\M-j" 'facemenu-justification-menu)
  262. (define-key enriched-mode-map "\M-S" 'set-justification-center)
  263. (define-key enriched-mode-map "\C-x\t" 'increase-left-margin)
  264. (define-key enriched-mode-map "\C-c\C-l" 'set-left-margin)
  265. (define-key enriched-mode-map "\C-c\C-r" 'set-right-margin)
  266.  
  267. ;;;
  268. ;;; Some functions dealing with text-properties, especially indentation
  269. ;;;
  270.  
  271. (defun enriched-map-property-regions (prop func &optional from to)
  272.   "Apply a function to regions of the buffer based on a text property.
  273. For each contiguous region of the buffer for which the value of PROPERTY is
  274. eq, the FUNCTION will be called.  Optional arguments FROM and TO specify the
  275. region over which to scan.
  276.  
  277. The specified function receives three arguments: the VALUE of the property in
  278. the region, and the START and END of each region."
  279.   (save-excursion
  280.     (save-restriction
  281.       (if to (narrow-to-region (point-min) to))
  282.       (goto-char (or from (point-min)))
  283.       (let ((begin (point))
  284.         end
  285.         (marker (make-marker))
  286.         (val (get-text-property (point) prop)))
  287.     (while (setq end (text-property-not-all begin (point-max) prop val))
  288.       (move-marker marker end)
  289.       (funcall func val begin (marker-position marker))
  290.       (setq begin (marker-position marker)
  291.         val (get-text-property marker prop)))
  292.     (if (< begin (point-max))
  293.         (funcall func val begin (point-max)))))))
  294.  
  295. (put 'enriched-map-property-regions 'lisp-indent-hook 1)
  296.  
  297. (defun enriched-insert-indentation (&optional from to)
  298.   "Indent and justify each line in the region."
  299.   (save-excursion
  300.     (save-restriction
  301.       (if to (narrow-to-region (point-min) to))
  302.       (goto-char (or from (point-min)))
  303.       (if (not (bolp)) (forward-line 1))
  304.       (while (not (eobp))
  305.     (if (eolp)
  306.         nil ; skip blank lines
  307.       (indent-to (current-left-margin))
  308.       (justify-current-line t nil t))
  309.     (forward-line 1)))))
  310.  
  311. ;;;
  312. ;;; Encoding Files
  313. ;;;
  314.  
  315. ;;;###autoload
  316. (defun enriched-encode (from to &optional orig-buf)
  317.   (if enriched-verbose (message "Enriched: encoding document..."))
  318.   (save-restriction
  319.     (narrow-to-region from to)
  320.     (delete-to-left-margin)
  321.     (unjustify-region)
  322.     (goto-char from)
  323.     (format-replace-strings '(("<" . "<<")))
  324.     (format-insert-annotations 
  325.      (format-annotate-region from (point-max) enriched-translations
  326.                  'enriched-make-annotation enriched-ignore))
  327.     (goto-char from)
  328.     (insert (if (stringp enriched-initial-annotation)
  329.         enriched-initial-annotation
  330.           (save-excursion
  331.         ;; Eval this in the buffer we are annotating.  This
  332.         ;; fixes a bug which was saving incorrect File-Width
  333.         ;; information, since we were looking at local
  334.         ;; variables in the wrong buffer.
  335.         (if orig-buf (set-buffer orig-buf))
  336.         (funcall enriched-initial-annotation))))
  337.     (enriched-map-property-regions 'hard
  338.       (lambda (v b e)
  339.     (if (and v (= ?\n (char-after b)))
  340.         (progn (goto-char b) (insert "\n"))))
  341.       (point) nil)
  342.     (if enriched-verbose (message nil))
  343.     ;; Return new end.
  344.     (point-max)))
  345.  
  346. (defun enriched-make-annotation (name positive)
  347.   "Format an annotation called NAME.
  348. If POSITIVE is non-nil, this is the opening annotation, if nil, this is the
  349. matching close."
  350.   (cond ((stringp name)
  351.      (format enriched-annotation-format (if positive "" "/") name))
  352.     ;; Otherwise it is an annotation with parameters, represented as a list
  353.     (positive
  354.      (let ((item (car name))
  355.            (params (cdr name)))
  356.        (concat (format enriched-annotation-format "" item)
  357.            (mapconcat (lambda (i) (concat "<param>" i "</param>"))
  358.                   params ""))))
  359.     (t (format enriched-annotation-format "/" (car name)))))
  360.  
  361. ;; XEmacs addition
  362. (defun enriched-face-strip-size (face)
  363.   "Create a symbol from the name of FACE devoid of size information,
  364. i.e. remove all larger- and smaller- prefixes."
  365.   (let* ((face-symbol (face-name face))
  366.      (face-name (symbol-name face-symbol))
  367.      (old-name face-name)
  368.      new-name)
  369.     (while
  370.     (not (string-equal
  371.           old-name
  372.           (setq new-name (replace-in-string old-name "^larger-" ""))))
  373.       (setq old-name new-name))
  374.     
  375.     (while
  376.     (not (string-equal
  377.           old-name
  378.           (setq new-name (replace-in-string old-name "^smaller-" ""))))
  379.       (setq old-name new-name))
  380.     
  381.     (if (string-equal new-name face-name)
  382.     face-symbol
  383.       (intern new-name))))
  384.  
  385. (defun enriched-encode-other-face (old new)
  386.   "Generate annotations for random face change.
  387. One annotation each for foreground color, background color, italic, etc."
  388.   (cons (and old (enriched-face-ans old))
  389.     (and new (enriched-face-ans new))))
  390.         
  391. (defun enriched-face-ans (face)
  392.   "Return annotations specifying FACE."
  393.   ;; XEmacs change (entire body of this function)
  394.   (let ((face-name (symbol-name face)))
  395.     (cond ((string-match "^fg:" face-name)
  396.        (list (list "x-color" (substring face-name 3))))
  397.       ((string-match "^bg:" face-name)
  398.        (list (list "x-bg-color" (substring face-name 3))))
  399.       ((or (string-match "^larger-" face-name)
  400.            (string-match "^smaller-" face-name))
  401.        (cdr (format-annotate-single-property-change
  402.          'face nil (enriched-face-strip-size face)
  403.          enriched-translations)))
  404.       (t
  405.        (let* ((fg (and (not (eq (face-foreground face)
  406.                     (face-foreground 'default)))
  407.                (color-name (face-foreground face))))
  408.           (bg (and (not (eq (face-background face)
  409.                     (face-background 'default)))
  410.                (color-name (face-background face))))
  411.           (ans '()))
  412.          (if fg (setq ans (cons (list "x-color" fg) ans)))
  413.          (if bg (setq ans (cons (list "x-bg-color" bg) ans)))
  414.          ans)))))
  415.  
  416. ;; XEmacs addition
  417. (defun enriched-size-annotation (n annotation)
  418.   "Generate ANNOTATION N times."
  419.   (let ((l '()))
  420.     (while (not (zerop n))
  421.       (setq l (cons annotation l))
  422.       (setq n (1- n)))
  423.     l))
  424.  
  425. ;; XEmacs addition
  426. (defun enriched-encode-size (old new)
  427.   "Return annotations specifying SIZE."
  428.   (let* ((old (or old 0))
  429.      (new (or new 0))
  430.      (closing-annotation
  431.       (enriched-size-annotation (abs old)
  432.                     (if (> old 0) "bigger" "smaller")))
  433.      (opening-annotation
  434.       (enriched-size-annotation (abs new)
  435.                     (if (> new 0) "bigger" "smaller"))))
  436.     (cons closing-annotation
  437.       opening-annotation)))
  438.  
  439. ;;;
  440. ;;; Decoding files
  441. ;;;
  442.  
  443. ;;;###autoload
  444. (defun enriched-decode (from to)
  445.   (if enriched-verbose (message "Enriched: decoding document..."))
  446.   (use-hard-newlines 1 'never)
  447.   (save-excursion
  448.     (save-restriction
  449.       (narrow-to-region from to)
  450.       (goto-char from)
  451.  
  452.       ;; Deal with header
  453.       (let ((file-width (enriched-get-file-width)))
  454.     (enriched-remove-header)
  455.  
  456.     ;; Deal with newlines
  457.     (while (search-forward-regexp "\n\n+" nil t)
  458.       (if (current-justification)
  459.           (delete-char -1))
  460.       (set-hard-newline-properties (match-beginning 0) (point)))
  461.  
  462.     ;; Translate annotations
  463.     (format-deannotate-region from (point-max) enriched-translations
  464.                   'enriched-next-annotation)
  465.  
  466.     ;; Indent or fill the buffer
  467.     (cond (file-width        ; File was filled to this width
  468.            (setq fill-column file-width)
  469.            (if enriched-verbose (message "Indenting..."))
  470.            (enriched-insert-indentation))
  471.           (t            ; File was not filled.
  472.            (if enriched-verbose (message "Filling paragraphs..."))
  473.            (fill-region (point-min) (point-max))))
  474.     (if enriched-verbose (message nil)))
  475.       (point-max))))
  476.  
  477. (defun enriched-next-annotation ()
  478.   "Find and return next text/enriched annotation.
  479. Any \"<<\" strings encountered are converted to \"<\".
  480. Return value is \(begin end name positive-p), or nil if none was found."
  481.   (while (and (search-forward "<" nil 1)
  482.           (progn (goto-char (match-beginning 0))
  483.              (not (looking-at enriched-annotation-regexp))))
  484.     (forward-char 1)
  485.     (if (= ?< (char-after (point)))
  486.     (delete-char 1)
  487.       ;; A single < that does not start an annotation is an error,
  488.       ;; which we note and then ignore.
  489.       (message "Warning: malformed annotation in file at %s" 
  490.            (1- (point)))))
  491.   (if (not (eobp))
  492.       (let* ((beg (match-beginning 0))
  493.          (end (match-end 0))
  494.          (name (downcase (buffer-substring 
  495.                   (match-beginning 2) (match-end 2))))
  496.          (pos (not (match-beginning 1))))
  497.     (list beg end name pos))))
  498.  
  499. (defun enriched-get-file-width ()
  500.   "Look for file width information on this line."
  501.   (save-excursion
  502.     (if (search-forward "Text-Width: " (+ (point) 1000) t)
  503.     (read (current-buffer)))))
  504.  
  505. (defun enriched-remove-header ()
  506.   "Remove file-format header at point."
  507.   (while (looking-at "^[-A-Za-z]+: .*\n")
  508.     (delete-region (point) (match-end 0)))
  509.   (if (looking-at "^\n")
  510.       (delete-char 1)))
  511.  
  512. (defun enriched-decode-foreground (from to &optional color)
  513.   ;; XEmacs change
  514.   (let ((face (facemenu-get-face (intern (concat "fg:" color)))))
  515.     (if (not face)
  516.     (progn
  517.       (make-face face)
  518.       (message "Warning: Color \"%s\" can't be displayed." color)))
  519.     (list from to 'face face)))
  520.  
  521. (defun enriched-decode-background (from to &optional color)
  522.   ;; XEmacs change
  523.   (let ((face (facemenu-get-face (intern (concat "bg:" color)))))
  524.     (if (not face)
  525.     (progn
  526.       (make-face face)
  527.       (message "Warning: Color \"%s\" can't be displayed." color)))
  528.     (list from to 'face face)))
  529.  
  530. ;;; enriched.el ends here
  531.